<?php

/**
 * @author     Thibaut Cromphaut, Gaïtano Boeraeve, Matthias Mercy
 * @copyright  Copyright (c) 2011 memorygame
 */
class Application_Model_WorldMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Worlds';
    }

    /**
     * Wereld met een bepaalde id ophalen
     * 
     * @param integer $id
     * @return Application_Model_World
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('world_id = ?' => $id));

        return $this->_toWorld($row);
    }

    /**
     * Alle Werelden ophalen
     * 
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();

        return $this->_toWorlds($rows);
    }
    
    /**
     * Alle Werelden met usergegevens ophalen
     * 
     * @return array
     */
    public function fetchAllForUser($userId)
    {        
        
        $table = $this->getDbTable();

        $select = $table->select()->setIntegrityCheck(false);
        $select->from('worlds')->join(array('userWorlds' => 'user_has_worlds'),'worlds.world_id=userWorlds.world_id')
               ->where('userWorlds.user_id LIKE :userId')
               ->bind( array(':userId' => $userId) )
               ->order('worlds.world_id ASC');
        /*if($limit !== false && $limit > 0)
        {
            $select->limit($limit);
        }*/
        $rows = $table->fetchAll($select);        

        //Zend_Debug::dump($this->_toUserWorlds($rows));
        
        return $this->_toWorlds($rows);
    }
    
    /**
     * Geef het aantal worlds terug
     * 
     * @return integer
     */
    public function getCount()
    {        
        $table = $this->getDbTable();
        $select = $table->select();
        $select->from($table, array('count(*) as count'));
        
        $rows = $table->fetchAll($select);
        
        return $rows[0]->count;        
    }

    /**
     * User opslaan
     *
     * @param Application_Model_World $world
     */
    public function save(Application_Model_World $world)
    {
        $data = array(
            'world_name'        => $world->Name,
            'world_description' => $world->Description,
            'world_pictureUrl'  =>$world->PictureUrl,
        );

        if (null == $world->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('world_id = ?' => $world->Id));
        }
    }
    
    /**
     * wereld verwijderen
     */
    public function delete($id)
    {   
        if (null != $id) {  
            $this->getDbTable()->delete(array('world_id = ?' => $id));
        } 
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_World
     */
    private function _toWorld(Zend_Db_Table_Row_Abstract $row = null)
    {
        $world = new Application_Model_World();

        if ($row) {
            $world->Id          = $row['world_id'];
            $world->Name        = $row['world_name'];
            $world->Description = $row['world_description'];
            $world->PictureUrl  = $row['world_pictureUrl'];            
            if(isset($row['userHasWorld_score']))
                $world->UserScore   = $row['userHasWorld_score'];                        
            if(isset($row['userHasWorld_score']))
                $world->UserUnlocked   = $row['userHasWorld_unlocked'];   
        }

        return $world;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toWorlds(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $worlds = array();

        if ($rows) {            
            foreach ($rows as $row) {
                $worlds[] = $this->_toWorld($row);
            }            
        }        
        return $worlds;
    }
    
    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    /*private function _toUserWorlds(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $worlds = array();

        if ($rows) {
            $worlds[] = $this->getCount();
            foreach ($rows as $row) {
                $worlds[] = $this->_toWorld($row);
            }            
        }        
        return $worlds;
    }*/

}
